import {
	SET_USERINFO,
	REMOVE_USERINFO,
	ADD_PAGE,
	LOAD_MORE,
	ADD_NUM,
	SUB_NUM,
	INIT_NUM,
	GET_TOTAL
} from "../type";

import {getPostlist,goldCoin} from "../../api/index";


export function getTotal(token){
	return function(dispatch){
		goldCoin(token).then(res => {
		    dispatch({
            	type:GET_TOTAL,
            	total:Math.floor(res.data.TotalGoldCoin / 2)
            })  
		})
	}
}


export function initNum(){
    return {
    	type:INIT_NUM
    }
}


export function addNum(){
	return {
		type:ADD_NUM
	}
}


export function subNum(){
	return {
		type:SUB_NUM
	}
}



export function addPage(){
     return {
     	type:ADD_PAGE
     }
}


export function getList(page,appkey){
    
    return function(dispatch){
        getPostlist(page,appkey)
        .then(res=>{
             dispatch({
             	type:LOAD_MORE,
             	data:res.data
             })
        })
    }

}







export function removeUser(){
	 return {
	 	 type:REMOVE_USERINFO
	 }
}


export function setUser(){
	return function(dispatch){
	
	    if(localStorage.getItem("worksawardUserinfo")){
	    	dispatch({
			   type:SET_USERINFO,
               userinfo:JSON.parse(localStorage.getItem("worksawardUserinfo"))
		    })
	    }else{
            dispatch({
			   type:SET_USERINFO,
               userinfo:{}
		    })
	    }
		
	}
	
}
